/******************************************************
 * Copyright (c) 2012, LIJUN All rights reserved *
 ******************************************************/

/**
 * Request接口
 * @author carlli
 * @encode utf-8
 * @version 1.0
 * @data 2012.9.24
 * @modify
 * -------------------------------------------------------------------
 * 
 * -------------------------------------------------------------------
 * @usage
 * -------------------------------------------------------------------
 *
 * -------------------------------------------------------------------
 */
Smart.define("request", function(S){
    /**
     * 过滤脚本
     * @param String str 字符串
     * @return String str 过滤后的字符串
     */
    function FilterScript(str){
        str = str || "";
        str = str.replace(/<.*>/g, ""); //过滤标签注入
        str = str.replace(/(java|vb|action)script/gi, ""); //过滤脚本注入
        str = str.replace(/[\"\'][\s ]*([^=\"\'\s ]+[\s ]*=[\s ]*[\"\']?[^\"\']+[\"\']?)+/gi, ""); //过滤HTML属性注入
        str = str.replace(/[\s ]/g, "&nbsp;"); //替换空格
        return str;
    };
    /**
     * URL解码
     * @param String str 字符串
     * @return String tmp 解码后的字符串
     */
    function Decode(str){
        var tmp = str;
        
        try{
            tmp = decodeURIComponent(str);
        }catch(e){}
        
        return tmp;
    };
    /**
     * URL编码
     * @param String str 字符串
     * @return String tmp 编码后的字符串
     */
    function Encode(str){
        return encodeURIComponent(str);
    };
    /**
     * 获取URL参数
     * @param String key 参数名
     * @param String url 自定义的URL，可不传，默认为当前的URL
     * @return String value 匹配的值，如果没匹配返回null
     */
    function GetParameter(key, url){
        var items = GetParameterValues(key, url);
        var size = items.length;
        var value = (size > 0 ? items[size - 1] : null);
        
        items = null;
        return value;
    };
    /**
     * 获取URL参数集
     * @param String key 键名
     * @param String  url 自定义的URL，可不传，默认为当前的URL
     * @return Array result 返回匹配key的所有值
     */
    function GetParameterValues(key, url){       
        var search = (url || document.location.search);
        var pattern = new RegExp("[\?&]"+key+"\=([^&#\?]*)", "g");
        var matcher = null
        var items = null;
        var result = [];

        while(null != (matcher = pattern.exec(search))){
            items = matcher[1];
            items = FilterScript(items);
            result.push(items);
        }
        pattern = null;
        return result;
    };
    /**
     * 将querystring转换成map对象
     * @param String qs URL查询串
     * @return Object items
     */
    function ToJSONObject(qs){
        var s = qs || document.location.search;
        var pattern = /[\?&]([^\?&=#]+)=([^&#\?]*)/g;
        var matcher = null;
        var items = {};

        while(null != (matcher = pattern.exec(s))){
            items[matcher[1]] = FilterScript(matcher[2]);
        }
        pattern = null; matcher = null;

        return items;
    };
    /**
     * 将paramters还原成querystring
     * @param Object o URL查询串MAP对象
     * @return String str
     */
    function ToJSONString(o){
        var tmp = [];
        var str = "";
        for(var name in o){
            if(o.hasOwnProperty(name)){
                tmp.push(name + "=" + o[name]);
            }
        }

        str = tmp.join("&");
        tmp = null; o = null;
        return str;
    };
    /**
     * 解析URL
     * @param String qs URL查询串
     * @return Object items {String url, String protocol, String host, String port, String pathname, String search, String hash}
     */
    function ParseURL(url){
        var p = /(([^\:\/\?&#]+\:)\/\/([^\.\/\:]+(\.[^\.\/\:]+)*)(\:([1-9][0-9]*))?)?([^\?&#]+)(\?[^#]*)?(#[\w\W]*)?/;
        var loc = document.URL;
        //[
        // "https://www.domain.com:8080/test/a.shtml?t=123&no=1#!req=001", 
        // "https://www.domain.com:8080", 
        // "https:", 
        // "www.domain.com", 
        // ".com", 
        // ":8080", 
        // "8080", 
        // "/test/a.shtml", 
        // "?t=123&no=1", 
        // "#!req=001"
        //]
        var r0 = p.exec(url);
        var r1 = p.exec(loc);
        var uri = r0[1];
        var o = {
            url:       url, 
            protocol:  ((uri ? r0[2] : r1[2]) || ""),
            host:      ((uri ? r0[3] : r1[3]) || ""), 
            port:      ((uri ? r0[6] : r1[6]) || ""), 
            pathname:  (r0[7] || "/"), 
            search:    (r0[8] || ""), 
            hash:      (r0[9] || "")
        };
        p = null;
        r0 = null;
        r1 = null;
        
        return o;
    };
    
    var __request = {
        filterScript : FilterScript,
        decode : Decode,
        encode : Encode,
        getParameter : GetParameter,
        getParameterValues : GetParameterValues,
        toJSONObject : ToJSONObject,
        toJSONString : ToJSONString,
        parseURL : ParseURL
    };
    
    S.source("Request", __request);
}); 